package com.novabluetooth.services.lbxd;

import com.ecg.collectapi.ConnectBoxMethod;
import com.health.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 理邦心电主界面
 * @author nova
 *
 */
public class LBXD_MainActivity extends Activity {

	private Button btnConnBox;//连接采集盒的按钮插件
	private ConnectBoxMethod mConnBox;
	private boolean isTimeOut = false;//连接采集盒的时候，花费时间太长，则变为true,返回提示信息
	private static final int OK = 1000;//采集盒连接成功
	private static final int TIMEMILLS = 4000;//4秒时间进行连接采集盒
	ProgressDialog mDialog;//声明等待控件对象
	private boolean isDemo = true;//false不是demo模式
	
	private Button btn_back;
	private Button btn_up;
	
	private TextView title;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*TelephonyManager tm = (TelephonyManager) this
				.getSystemService(TELEPHONY_SERVICE);
		String codeString = tm.getDeviceId();//获取移动设备的唯一标示码
*/		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.lbxd_activity_main);
		initView();
		setListens();
	}
	//创建Handler对象，响应线程结束后的操作
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what == OK){
				mDialog.dismiss();//关闭等待控件
				//如果已经连接上采集盒
				if(mConnBox.isConnectedBox()){
					Intent intent = new Intent(LBXD_MainActivity.this , LBXD_DisplayWaveActivity.class);
					startActivity(intent);
					Toast.makeText(getBaseContext(), "连接采集盒成功", Toast.LENGTH_SHORT).show();
					LBXD_MainActivity.this.finish();
					
				}else{
					if(isDemo){
						Intent intent = new Intent(LBXD_MainActivity.this , LBXD_DisplayWaveActivity.class);
						intent.putExtra("isDemo", true);
						startActivity(intent);
						Toast.makeText(getBaseContext(), "Demo模式", Toast.LENGTH_SHORT).show();
						LBXD_MainActivity.this.finish();
					}else{
						Toast.makeText(getBaseContext(), "连接采集盒失败，请重新连接", Toast.LENGTH_SHORT).show();
					}
				}
			}
		};
	};
	
	
	public void initView(){
		btn_back=(Button)findViewById(R.id.btn_back);
		btn_up=(Button)findViewById(R.id.btn_up);
		btn_up.setVisibility(View.GONE);
		
	    title=(TextView)findViewById(R.id.title);
	    title.setText("理邦心电");
		
		
		
		//与Button插件进行绑定
		btnConnBox = (Button) findViewById(R.id.btnConn);
		//初始化mConnBox变量
		mConnBox = new ConnectBoxMethod(this);
		//如果没有开启蓝牙，提示开启蓝牙
		if(!mConnBox.isOpenBT()){
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivity(enableBtIntent);
		}
		btnConnBox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//创建一个连接采集盒的线程
				Thread mThread = new Thread(){
					@Override
					public void run() {
						//连接采集盒
						mConnBox.doConnToDevice();
						mHandler.sendEmptyMessageDelayed(OK, TIMEMILLS);
					}
				};
				mDialog = new ProgressDialog(LBXD_MainActivity.this);
				mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				mDialog.setMessage("连接采集盒中...");
				mDialog.setIndeterminate(false);
				mDialog.setCancelable(false);
				mThread.start();mDialog.show();
				
			}
		});
	}
	
	
	
	private void setListens(){
		btn_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LBXD_MainActivity.this.finish();
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		finish();
		return super.onKeyDown(keyCode, event);
	}
}
